package com.sinosoft.dsp.platform.components.system;

import java.io.Serializable;

import org.primefaces.util.ArrayUtils;

import com.sinosoft.dsp.platform.components.AbstractViewBean;
import com.sinosoft.dsp.platform.services.PlatformViewService;
import com.sinosoft.dsp.platform.utils.MessageBundles;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.helper.ToHelper;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 标签对象类，用来表示标签对象元素
 * 
 * @since 1.0 2009-12-18
 * @author 潘巍（Peter Pan）
 * 
 */
public class TabBean extends AbstractViewBean implements Serializable, ITabBean {

	private static final long serialVersionUID = -2364707446916385345L;

	// 是否可以关闭
	public static final String ATTRIBUTE_CLOSABLE = "closable";
	// 16*16图片
	public static final String ATTRIBUTE_IMAGE16 = "image16";
	// 标签文字
	public static final String ATTRIBUTE_LABEL = "label";
	// 内容
	public static final String ATTRIBUTE_SRC = "src";

	public static final String[] ATTRIBUTE_STRING = new String[] {
			ATTRIBUTE_IMAGE16, ATTRIBUTE_LABEL };

	public static final String[] ATTRIBUTE_BOOLEAN = new String[] { ATTRIBUTE_CLOSABLE };

	public static final String[] ATTRIBUTE = ArrayUtils.concat(
			ATTRIBUTE_STRING, ATTRIBUTE_BOOLEAN);

	@Override
	public void construct(String sceneId, String layout) throws SinoException {
		super.construct(sceneId, layout);
		PlatformViewService view = SinoManagedBean
				.findBean("platformViewService");
		if (layout != null) {
			putAttr(ATTRIBUTE_LABEL, view.getAttributeValueFromLayout(this
					.getLayout(), "displayName"));
			putAttr(ATTRIBUTE_IMAGE16, view.getAttributeValueFromLayout(this
					.getLayout(), ATTRIBUTE_IMAGE16));
			putAttr(ATTRIBUTE_CLOSABLE, view.getAttributeValueFromLayout(this
					.getLayout(), ATTRIBUTE_CLOSABLE));
		}
		if (Const.isEmpty(getAttr(ATTRIBUTE_LABEL)))
			putAttr(ATTRIBUTE_LABEL, "");
		if (Const.isEmpty(getAttr(ATTRIBUTE_IMAGE16)))
			putAttr(ATTRIBUTE_IMAGE16, Const.DEFAULT_IMAGE16);
		if (Const.isEmpty(getAttr(ATTRIBUTE_CLOSABLE)))
			putAttr(ATTRIBUTE_CLOSABLE, Boolean.TRUE.toString());
	}

	@Override
	public void init(IGenericTO tos) throws SinoException {
		super.init(tos);

		// 通过tos来赋值
		for (String attr : ATTRIBUTE) {
			Attribute<?> a = ToHelper.getAttributeByPathSilent(tos, attr);
			if (a != null)
				putAttr(attr, a.convertToString());
		}

	}

	@Override
	public String getLabelString() {
		MessageBundles mb = SinoManagedBean.findBean("messageBundles");
		return mb.getValue(getAttr(ATTRIBUTE_LABEL), getAttr(ATTRIBUTE_LABEL));
	}

	@Override
	public boolean hasParent() {
		return true;
	}

}